package com.team06.dao;

import com.team06.pojo.Onre;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class OnreDao {
    private static final Logger LOGGER = LoggerFactory.getLogger(OnreDao.class.getName());
    private final JdbcTemplate jdbcTemplate;

    public OnreDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    public void insertMessage(Onre entity) {
        String sql = "INSERT INTO MSG_DFME_ONRE (" +
                "META_SNDR, META_RCVR, META_SEQN, META_DDTM, META_TYPE, META_STYP, " +
                "DFLT_FLID, DFLT_FFID, DFLT_FIDE, DFLT_FATT, DFLT_STAT, " +
                "DFLT_ISTA, DFLT_MSTA, DFLT_MIST, DFLT_PAST, DFLT_ELDT) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

        try {
            jdbcTemplate.update(sql,
                    entity.getMetaSnDR(),
                    entity.getMetaRCVR(),
                    entity.getMetaSEQN(),
                    entity.getMetaDDTM(),
                    entity.getMetaTYPE(),
                    entity.getMetaSTYP(),
                    entity.getDfltFLID(),
                    entity.getDfltFFID(),
                    entity.getDfltFIDE(),
                    entity.getDfltFATT(),
                    entity.getDfltSTAT(),
                    entity.getDfltISTA(),
                    entity.getDfltMSTA(),
                    entity.getDfltMIST(),
                    entity.getDfltPAST(),
                    entity.getDfltELDT()
            );
            LOGGER.info("数据插入成功（通过 Spring JDBC）");
        } catch (Exception e) {
            LOGGER.error("Failed to insert message into database", e);
            throw new RuntimeException("Failed to insert message into database", e);
        }
    }
}